<?php

namespace common\models;

use Yii;
use mdm\admin\components\Configs;

/**
 * 后台管理员用户模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class AdminUser extends \common\base\User {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return Configs::instance()->userTable;
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['username', 'mobile', 'auth_key', 'password_hash', 'realname'], 'required'],
            [['status', 'validated_at', 'created_at', 'updated_at'], 'integer'],
            [['username', 'realname'], 'string', 'max' => 50],
            [['mobile'], 'string', 'max' => 11],
            [['email'], 'string', 'max' => 64],
            [['avatar', 'password_hash', 'password_reset_token', 'profile'], 'string', 'max' => 255],
            [['auth_key'], 'string', 'max' => 32],
            [['username'], 'unique'],
            [['mobile'], 'unique'],
            [['password_reset_token'], 'unique'],
            ['status', 'default', 'value' => self::STATUS_DEFAULT],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DEFAULT, self::STATUS_DELETED]],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'username' => '用户名',
            'mobile' => '手机号',
            'email' => '邮箱',
            'avatar' => '头像',
            'auth_key' => '密钥',
            'password_hash' => 'HASH密码',
            'password_reset_token' => '密码重置TOKEN',
            'realname' => '真实姓名',
            'profile' => '个人简介',
            'rolename' => '管理员角色',
            'status' => '状态',
            'validated_at' => '校验时间',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    /**
     * @inheritdoc
     */
    public function getRolename() {
        $manager = Yii::$app->getAuthManager();
        $roles = $manager->getRolesByUser($this->id);
        $rolename = '';
        foreach ($roles as $role) {
            $rolename .= $role->name . ' ';
        }
        return $rolename;
    }

    public function getPortrait() {
        
    }

    public function getUserName() {
        return $this->username;
    }

}
